﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FastData;
using FastCtr;

namespace InjectMachine
{
    public class InjectPro : InjectOmroPLC
    {
        public event Action<string, object> Scanner1DoWorkEvent;
        public event Action<string, object> Scanner2DoWorkEvent;
        public event Action<string, object> Scanner3DoWorkEvent;
        public event Action<string, object> Scanner4DoWorkEvent;
        public event Action<string, object> Scanner5DoWorkEvent;
        public event Action<string, object> Scanner6DoWorkEvent;
        public event Action<string, object> Scanner7DoWorkEvent;
        public event Action<string, WeighInfoPLCaddress> ANDWeight1DoWorkEvent;
        public event Action<string, WeighInfoPLCaddress> ANDWeight2DoWorkEvent;
        public event Action<string, WeighInfoPLCaddress> ANDWeight3DoWorkEvent;
        public event Action<string, WeighInfoPLCaddress> ANDWeight4DoWorkEvent;
        public event Action<string, WeighInfoPLCaddress> ANDWeight5DoWorkEvent;
        public event Action<string, WeighInfoPLCaddress> ANDWeight6DoWorkEvent;
        public event Action<string, WeighInfoPLCaddress> ANDWeight7DoWorkEvent;
        public event Action<string, WeighInfoPLCaddress> ANDWeight8DoWorkEvent;
        public InjectPro() : base()
        {
            OnPlcReceivedValueChanged += InjectPro_OnPlcReceivedValueChanged;
        }

        protected virtual void Scanner1DoWork(string name, object value)
        {
            Scanner1DoWorkEvent?.Invoke(name, $"{PCScanID}[0]");
        }
        protected virtual void Scanner2DoWork(string name, object value)
        {
            Scanner2DoWorkEvent?.Invoke(name, $"{PCScanID}[1]");
        }
        protected virtual void Scanner3DoWork(string name, object value)
        {
            Scanner3DoWorkEvent?.Invoke(name, $"{PCScanID}[2]");
        }
        protected virtual void Scanner4DoWork(string name, object value)
        {
            Scanner4DoWorkEvent?.Invoke(name, $"{PCScanID}[3]");
        }
        protected virtual void Scanner5DoWork(string name, object value)
        {
            Scanner5DoWorkEvent?.Invoke(name, $"{PCScanID}[4]");
        }
        protected virtual void Scanner6DoWork(string name, object value)
        {
            Scanner6DoWorkEvent?.Invoke(name, $"{PCScanID}[5]");
        }
        protected virtual void Scanner7DoWork(string name, object value)
        {
            Scanner7DoWorkEvent?.Invoke(name, $"{PCScanID}[6]");
        }
        protected virtual void ANDWeight1DoWork(string name, object value)
        {
            ANDWeight1DoWorkEvent?.Invoke(name, new WeighInfoPLCaddress(name, $"{PCWeighID}[0]", $"{PCWeighValue}[0]", $"{PCWeighInjectionValue}[0]", $"{PCWeighDatabaseUpdateOK}[0]"));
        }
        protected virtual void ANDWeight2DoWork(string name, object value)
        {
            ANDWeight2DoWorkEvent?.Invoke(name, new WeighInfoPLCaddress(name, $"{PCWeighID}[1]", $"{PCWeighValue}[1]", $"{PCWeighInjectionValue}[1]", $"{PCWeighDatabaseUpdateOK}[1]"));
        }
        protected virtual void ANDWeight3DoWork(string name, object value)
        {
            ANDWeight3DoWorkEvent?.Invoke(name, new WeighInfoPLCaddress(name, $"{PCWeighID}[2]", $"{PCWeighValue}[2]", $"{PCWeighInjectionValue}[2]", $"{PCWeighDatabaseUpdateOK}[2]"));
        }
        protected virtual void ANDWeight4DoWork(string name, object value)
        {
            ANDWeight4DoWorkEvent?.Invoke(name, new WeighInfoPLCaddress(name, $"{PCWeighID}[3]", $"{PCWeighValue}[3]", $"{PCWeighInjectionValue}[3]", $"{PCWeighDatabaseUpdateOK}[3]"));
        }
        protected virtual void ANDWeight5DoWork(string name, object value)
        {
            ANDWeight5DoWorkEvent?.Invoke(name, new WeighInfoPLCaddress(name, $"{PCWeighID}[4]", $"{PCWeighValue}[4]", $"{PCWeighInjectionValue}[4]", $"{PCWeighDatabaseUpdateOK}[4]"));
        }
        protected virtual void ANDWeight6DoWork(string name, object value)
        {
            ANDWeight6DoWorkEvent?.Invoke(name, new WeighInfoPLCaddress(name, $"{PCWeighID}[5]", $"{PCWeighValue}[5]", $"{PCWeighInjectionValue}[5]", $"{PCWeighDatabaseUpdateOK}[5]"));
        }
        protected virtual void ANDWeight7DoWork(string name, object value)
        {
            ANDWeight7DoWorkEvent?.Invoke(name, new WeighInfoPLCaddress(name, $"{PCWeighID}[6]", $"{PCWeighValue}[6]", $"{PCWeighInjectionValue}[6]", $"{PCWeighDatabaseUpdateOK}[6]"));
        }
        protected virtual void ANDWeight8DoWork(string name, object value)
        {
            ANDWeight8DoWorkEvent?.Invoke(name, new WeighInfoPLCaddress(name, $"{PCWeighID}[7]", $"{PCWeighValue}[7]", $"{PCWeighInjectionValue}[7]", $"{PCWeighDatabaseUpdateOK}[7]"));
        }

        protected virtual void InjectPro_OnPlcReceivedValueChanged(string arg1, object arg2)
        {
            ListenVar listenVar = PlcListenArryNameList.Value.Where(t => arg1.Contains(t.Name)).FirstOrDefault();
            if (listenVar != null)
            {
                switch (arg1)
                {
                    case "PCScanSend[0]":
                        if (arg2.ToString() == "1")
                        {
                            Scanner1DoWork(arg1, arg2);
                        }
                        break;

                    case "PCScanSend[1]":
                        if (arg2.ToString() == "1")
                        {
                            Scanner2DoWork(arg1, arg2);
                        }
                        break;

                    case "PCScanSend[2]":
                        if (arg2.ToString() == "1")
                        {
                            Scanner3DoWork(arg1, arg2);
                        }
                        break;

                    case "PCScanSend[3]":
                        if (arg2.ToString() == "1")
                        {
                            Scanner4DoWork(arg1, arg2);
                        }
                        break;

                    case "PCScanSend[4]":
                        if (arg2.ToString() == "1")
                        {
                            Scanner5DoWork(arg1, arg2);
                        }
                        break;

                    case "PCScanSend[5]":
                        if (arg2.ToString() == "1")
                        {
                            Scanner6DoWork(arg1, arg2);
                        }
                        break;

                    case "PCScanSend[6]":
                        if (arg2.ToString() == "1")
                        {
                            Scanner7DoWork(arg1, arg2);
                        }
                        break;
                    case "PCWeighSend[0]":
                        if (arg2.ToString() == "1")
                        {
                            ANDWeight1DoWork(arg1, arg2);
                        }
                        break;
                    case "PCWeighSend[1]":
                        if (arg2.ToString() == "1")
                        {
                            ANDWeight2DoWork(arg1, arg2);
                        }
                        break;
                    case "PCWeighSend[2]":
                        if (arg2.ToString() == "1")
                        {
                            ANDWeight3DoWork(arg1, arg2);
                        }
                        break;
                    case "PCWeighSend[3]":
                        if (arg2.ToString() == "1")
                        {
                            ANDWeight4DoWork(arg1, arg2);
                        }
                        break;
                    case "PCWeighSend[4]":
                        if (arg2.ToString() == "1")
                        {
                            ANDWeight5DoWork(arg1, arg2);
                        }
                        break;
                    case "PCWeighSend[5]":
                        if (arg2.ToString() == "1")
                        {
                            ANDWeight6DoWork(arg1, arg2);
                        }
                        break;
                    case "PCWeighSend[6]":
                        if (arg2.ToString() == "1")
                        {
                            ANDWeight7DoWork(arg1, arg2);
                        }
                        break;
                    case "PCWeighSend[7]":
                        if (arg2.ToString() == "1")
                        {
                            ANDWeight8DoWork(arg1, arg2);
                        }
                        break;
                }
                WriteVariable(arg1, 3);
            }

        }
    }

    public class WeighInfoPLCaddress
    {
        public WeighInfoPLCaddress(string _TriggerName, string _IDAddress, string _WeighAddress, string _InjectionAddress, string _DataBaseOKAddress)
        {
            TriggerName = _TriggerName;
            IDAddress = _IDAddress;
            WeighAddress = _WeighAddress;
            InjectionAddress = _InjectionAddress;
            DataBaseOKAddress = _DataBaseOKAddress;
        }
        public string TriggerName { get; set; }
        public string IDAddress { get; set; }
        public string WeighAddress { get; set; }
        public string InjectionAddress { get; set; }
        public string DataBaseOKAddress { get; set; }
    }
}
